package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysUserStateBo;
import org.dromara.system.domain.vo.SysUserStateVo;
import org.dromara.system.service.ISysUserStateService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户状态信息
 *
 * @author Lion Li
 * @date 2025-02-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/user/state")
public class SysUserStateController extends BaseController {

    private final ISysUserStateService sysUserStateService;

    /**
     * 查询用户状态信息列表
     */
    @SaCheckPermission("system::state:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserStateVo> list(SysUserStateBo bo, PageQuery pageQuery) {
        return sysUserStateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户状态信息列表
     */
    @SaCheckPermission("system::state:export")
    @Log(title = "用户状态信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserStateBo bo, HttpServletResponse response) {
        List<SysUserStateVo> list = sysUserStateService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户状态信息", SysUserStateVo.class, response);
    }

    /**
     * 获取用户状态信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system::state:query")
    @GetMapping("/{id}")
    public R<SysUserStateVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(sysUserStateService.queryById(id));
    }

    /**
     * 新增用户状态信息
     */
    @SaCheckPermission("system::state:add")
    @Log(title = "用户状态信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysUserStateBo bo) {
        return toAjax(sysUserStateService.insertByBo(bo));
    }

    /**
     * 修改用户状态信息
     */
    @SaCheckPermission("system::state:edit")
    @Log(title = "用户状态信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysUserStateBo bo) {
        return toAjax(sysUserStateService.updateByBo(bo));
    }

    /*
     * 改变用户专题
     */

    @SaCheckPermission("system::state:edit")
    @Log(title = "用户状态信息变更", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@Validated(EditGroup.class) @RequestBody SysUserStateBo bo) {
        return toAjax(sysUserStateService.changeStatus(bo));
    }

    /**
     * 删除用户状态信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system::state:remove")
    @Log(title = "用户状态信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(sysUserStateService.deleteWithValidByIds(List.of(ids), true));
    }
}
